package com.newtouch.bxzs.enterprise.util;

import cn.hutool.core.date.DateUtil;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.util.CheckCertificateTypeUtil;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 客户推送接收字段校验
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/3/26 15:59</td>
 * <td>yuanjie</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author yuanjie
 * @version 1.0.0
 * @since 1.0.0
 */
public class FieldCheckUtils {

    private static final Integer YEAR_DAY = 365;

    private static final Integer INTENTION_REGISTRATION_DESCRIPTION_LENGTH = 500;
    private static final Integer POLICY_NUMBER_LENGTH = 50;


    public static void checkPolicyNumber(String policyNumber){
        boolean matches = policyNumber.matches("^[a-z0-9A-Z]+$");
        if (!matches){
            throw new BaseException("-1", "不存在中文保单号");
        }
        if (policyNumber.getBytes().length > POLICY_NUMBER_LENGTH){
            throw new BaseException("-1", "保单号不能大于50字符");
        }
    }

    //保单服务意向推送查询
    public static void checkPolicyPush(Date pushStartTime,Date pushEndTime,String policyNumber,String customerCertificateType,String customerCertificateNumber){
        //校验起止日期是否大于365天
        if (DateUtil.betweenDay(pushStartTime,pushEndTime,true) > YEAR_DAY){
            throw new BaseException("-1","时间范围不能超过365天");
        }
        if (DateUtil.compare(pushStartTime,pushEndTime) > 0){
            throw new BaseException("-1","推送起期不能小于推送止期");
        }
        if (!StringUtils.isEmpty(policyNumber)){
            boolean matches = policyNumber.matches("^[a-z0-9A-Z]+$");
            if (!matches){
                throw new BaseException("-1", "不存在中文保单号");
            }
        }
        //判断证件类型证件号
        if (!StringUtils.isEmpty(customerCertificateType) && !StringUtils.isEmpty(customerCertificateNumber)){
            CheckCertificateTypeUtil.checkCertificate(customerCertificateType,customerCertificateNumber);
        }else if (StringUtils.isEmpty(customerCertificateType) && StringUtils.isEmpty(customerCertificateNumber)){

        }else {
            throw new BaseException("-1", "证件类型和证件号必须都填写或不填");
        }
    }

    public static void checkAddPolicyPush(String policyNumber,String intentionRegistrationDescription){
        checkPolicyNumber(policyNumber);
        if (!StringUtils.isEmpty(intentionRegistrationDescription)){
            if (intentionRegistrationDescription.getBytes().length > INTENTION_REGISTRATION_DESCRIPTION_LENGTH){
                throw new BaseException("-1", "任务详情不能大于50字符");
            }
        }
    }
}
